package tongcheng.com.tongcheng.base.main.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.bigkoo.pickerview.listener.CustomListener;
import com.yanzhenjie.nohttp.RequestMethod;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import butterknife.Bind;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.bean.CardBean;
import tongcheng.com.tongcheng.bean.StatusBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.ui.yemian.login.activity.LoginActivity;
import tongcheng.com.tongcheng.utils.AppManager;
import tongcheng.com.tongcheng.utils.FileCacheUtils;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.utils.Setting;
import tongcheng.com.tongcheng.utils.ShowToast;

public class SettingActivity extends BaseActivity {

    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.title_bar)
    RelativeLayout mBg;
    @Bind(R.id.setting_ll1)
    LinearLayout mSettingLl1;
    @Bind(R.id.setting_ll2)
    LinearLayout mSettingLl2;
    @Bind(R.id.setting_outlogin)
    Button mSettingOutlogin;
    @Bind(R.id.ll_guanYuWoMen)
    LinearLayout ll_guanYuWoMen;
    @Bind(R.id.ll_yongHuXieYi)
    LinearLayout ll_yongHuXieYi;
    @Bind(R.id.ll_yaJinShuoMing)
    LinearLayout ll_yaJinShuoMing;
    @Bind(R.id.ll_chongZhiXieYi)
    LinearLayout ll_chongZhiXieYi;
    @Bind(R.id.ll_gongSiGuiMo)
    LinearLayout ll_gongSiGuiMo;
    @Bind(R.id.ll_qingLiHuanCun)
    LinearLayout ll_qingLiHuanCun;
    @Bind(R.id.view_guimo)
    View view_guimo;
    @Bind(R.id.bt_zhuXiao)
    Button bt_zhuXiao;

    private int yongHuXieYi;
    ArrayList<String> list = new ArrayList<String>();


    @Override
    public Integer initResid() {
        return R.layout.activity_setting2;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {
        settitlebar(mBg);
        mTitle.setText("设置");

        int shenfen = Setting.getShenfen();
        if(shenfen == 3){
            ll_gongSiGuiMo.setVisibility(View.VISIBLE);
            view_guimo.setVisibility(View.VISIBLE);
        }

        //查询是否有未完成订单
        getChaXunDingDan();

        //注销身份
        bt_zhuXiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ZhuXiaoShenFen();
            }
        });

        yongHuXieYi = getIntent().getIntExtra("yongHuXieYi",0);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //退出登录
        mSettingOutlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                OutLogin();
            }
        });

        ll_gongSiGuiMo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                list.clear();
                cardItem.clear();
                getGuiMoData();

            }
        });

        //清理缓存
        ll_qingLiHuanCun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FileCacheUtils.cleanApplicationData(SettingActivity.this,getExternalCacheDir().getPath());
                AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                builder.setTitle("提示信息").setIcon(android.R.drawable.ic_dialog_alert);
                builder.setMessage("清理成功");
                builder.create().show();//创建并展示对话框

            }
        });

        ll_guanYuWoMen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent xnIntent = new Intent(SettingActivity.this,WebViewActivity.class);
                xnIntent.putExtra("title","关于我们");
                xnIntent.putExtra("keyword",1);
                startActivity(xnIntent);
            }
        });

        ll_yongHuXieYi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent xnIntent = new Intent(SettingActivity.this,WebViewActivity.class);
                xnIntent.putExtra("title","用户协议");
                xnIntent.putExtra("keyword",yongHuXieYi);
                startActivity(xnIntent);
            }
        });

        ll_yaJinShuoMing.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent xnIntent = new Intent(SettingActivity.this,WebViewActivity.class);
                xnIntent.putExtra("title","押金说明");
                xnIntent.putExtra("keyword",7);
                startActivity(xnIntent);
            }
        });

        ll_chongZhiXieYi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent xnIntent = new Intent(SettingActivity.this,WebViewActivity.class);
                xnIntent.putExtra("title","充值协议");
                xnIntent.putExtra("keyword",8);
                startActivity(xnIntent);
            }
        });

        initCustomOptionPicker();

    }

    private void ZhuXiaoShenFen() {
        AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
        builder.setTitle("提示信息").setIcon(android.R.drawable.ic_dialog_alert);
        builder.setMessage("本次操作注销可以重新选择身份，该操作不可恢复");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                ZhuXiao();
            }
        });
        builder.setNeutralButton("取消", null);
        builder.create().show();//创建并展示对话框
    }

    //注销身份
    private void ZhuXiao() {
        MyRequest request = new MyRequest(ConStans.ZhuXiaoShenFenUrl, RequestMethod.GET);
        request.add("userid",Setting.getId());
        CallServer.getInstance().addGet(3, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                Log.e("注销身份",json + "");
                try {
                    JSONObject object = new JSONObject(json);
                    if(object.getInt("status") == 1){

                        String text = object.getString("msg");
                        ShowToast.showToast(text);
                        Setting.clear();
                        AppManager.getAppManager().finishAllActivity();
                        startactivity(SettingActivity.this, LoginActivity.class);

                    }else if(object.getInt("status") == 0){
                        String text = object.getString("msg");
                        AlertDialog.Builder builder = new AlertDialog.Builder(SettingActivity.this);
                        builder.setTitle("提示信息").setIcon(android.R.drawable.ic_dialog_alert);
                        builder.setMessage(text);
                        builder.create().show();//创建并展示对话框
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, request);
    }


    //查询是否有未完成订单
    private void getChaXunDingDan() {

    }


    //判断是第几个条目点中的
    int tag = 0;
    private OptionsPickerView pvCustomOptions;
    private ArrayList<CardBean> cardItem = new ArrayList<>();

    //公司规模梯度
    private void getGuiMoData() {

        MyRequest request = new MyRequest(ConStans.GongSiGuiMoUrl,RequestMethod.GET);
        CallServer.getInstance().addGet(3, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                Log.e("公司规模",json);
                try {
                    JSONObject object = new JSONObject(json);
                    if(object.getInt("status") == 1){
                        JSONArray array = object.getJSONArray("data");
                        if(array.length() > 0){
                            for(int i=0;i<array.length();i++){
                                String text = array.getString(i);
//                                list.add(text);
                                cardItem.add(new CardBean(i,text));
                                Log.e("公司规模",text);

                            }

                            tag = 1;
                            pvCustomOptions.show();

//                            View mView = LayoutInflater.from(SettingActivity.this).inflate(R.layout.layout_gongsi_guimo, null);
//                            ListView lv_guiMo = mView.findViewById(R.id.lv_guiMo);
//                            lv_guiMo.setAdapter(new GuiMoAdapter(SettingActivity.this,list));
//                            final PopupWindow mPopupWindow = new PopupWindow(mView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
//                            mPopupWindow.setOutsideTouchable(true);
//                            Drawable drawable = new ColorDrawable(Color.alpha(R.color.transparent)); //透明色
//                            mPopupWindow.setBackgroundDrawable(drawable);
//                            mPopupWindow.showAtLocation(ll_gongSiGuiMo, Gravity.BOTTOM, 0, 0);

                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }, request);


    }

    //条件选择器初始化，自定义布局
    private void initCustomOptionPicker() {
        /**
         * @description
         *
         * 注意事项：
         * 自定义布局中，id为 optionspicker 或者 timepicker 的布局以及其子控件必须要有，否则会报空指针。
         * 具体可参考demo 里面的两个自定义layout布局。
         */
        pvCustomOptions = new OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //选中位置
                String tx = cardItem.get(options1).getPickerViewText();
                if (tag == 1) {
//                    mGerenxinxiGuimo.setText(tx);
                    tag = 0;
                }

            }
        }).setLayoutRes(R.layout.pickerview_custom_options11, new CustomListener() {
            @Override
            public void customLayout(View v) {
                final TextView tvCancel = (TextView) v.findViewById(R.id.tv1);
                final TextView tvOK = (TextView) v.findViewById(R.id.tv2);
                tvOK.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pvCustomOptions.returnData();
                        pvCustomOptions.dismiss();
                    }
                });
                tvCancel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        pvCustomOptions.dismiss();
                    }
                });
            }
        })
                .isDialog(true)
                .build();

        pvCustomOptions.setPicker(cardItem);//添加数据
    }


    //退出登录
    private void OutLogin() {
        MyRequest request = new MyRequest(ConStans.OutLogin_Url, RequestMethod.POST);
        request.add("phone", Setting.getPhone());
        CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {
                Log.e("退出",json);
                StatusBean statusBean = GsonUtil.GsonToBean(json, StatusBean.class);
                if (statusBean != null) {
                    ShowToast.showToast(statusBean.msg);
                    if (statusBean.status == 1) {
                        Setting.clear();
                        finish();
                        AppManager.getAppManager().finishAllActivity();
                        startactivity(SettingActivity.this, LoginActivity.class);

                    }

                }
            }
        });
    }

    public class GuiMoAdapter extends BaseAdapter{
        Context mContext ;
        ArrayList<String> list;

        public GuiMoAdapter(Context mContext, ArrayList<String> list) {
            this.mContext = mContext;
            this.list = list;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view1 = LayoutInflater.from(mContext).inflate(R.layout.item_gongsi_guimo,null);
            TextView tv_text = view1.findViewById(R.id.tv_text);
            String text = list.get(i);
            tv_text.setText(text);
            return view1;
        }
    }

}
